package com.zl.autocode.service.autocode.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.dao.autocode.ZlFieldMappingDtlMapper;
import com.zl.autocode.dao.autocode.ZlFieldMappingMapper;
import com.zl.autocode.dto.autocode.fieldMapping.ReqFieldMappingAddDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ReqFieldMappingDelDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ReqFieldMappingDetailDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ReqFieldMappingEditDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ReqFieldMappingQueryDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ReqFieldMappingStateDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ResFieldMappingAddDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ResFieldMappingDelDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ResFieldMappingDetailDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ResFieldMappingEditDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ResFieldMappingQueryDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ResFieldMappingStateDTO;
import com.zl.autocode.dto.autocode.fieldMapping.ResFieldMappingVO2;
import com.zl.autocode.entity.autocode.ZlFieldMapping;
import com.zl.autocode.entity.autocode.po.ZlFieldMappingVo;
import com.zl.autocode.service.autocode.IFieldMappingService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 字段映射
 * @author ZL
 */
@Service("autocodeFieldMappingService")
public class FieldMappingServiceImpl extends AutocodeBaseManager implements IFieldMappingService {

    @Autowired
    private ZlFieldMappingMapper fieldMappingMapper;
    @Autowired
    private ZlFieldMappingDtlMapper fieldMappingDtlMapper;

    @Override
    public ResFieldMappingQueryDTO queryFieldMappingList(ReqDTO<ReqFieldMappingQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResFieldMappingQueryDTO resDTO = BeanUtil.copyProperties(page, ResFieldMappingQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getMappingTitle())){
            reqData.setMappingTitle("%"+reqData.getMappingTitle()+"%");
        }
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData,false,true);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlFieldMappingVo> list = fieldMappingMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResFieldMappingVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResFieldMappingDetailDTO queryFieldMappingDetail(ReqDTO<ReqFieldMappingDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingDetailDTO reqData = reqInfo.getReqData();
        ResFieldMappingDetailDTO res = new ResFieldMappingDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlFieldMapping view = fieldMappingMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResFieldMappingAddDTO insertFieldMappingInfo(ReqDTO<ReqFieldMappingAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingAddDTO reqData = reqInfo.getReqData();
        ResFieldMappingAddDTO res = new ResFieldMappingAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlFieldMapping record = new ZlFieldMapping();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setMappingId(IdUtil.simpleUUID());
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("mappingId",record.getMappingId());
        //检查主键是否冲突
        if(fieldMappingMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        fieldMappingMapper.insertSelective(record);
        
        ZlFieldMapping keyInfo = fieldMappingMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResFieldMappingEditDTO updateFieldMappingInfo(ReqDTO<ReqFieldMappingEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingEditDTO reqData = reqInfo.getReqData();
        ResFieldMappingEditDTO res = new ResFieldMappingEditDTO();
        Date time = getSystemTime().getDate();
        
        ZlFieldMapping record = new ZlFieldMapping();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        fieldMappingMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("mappingId",record.getMappingId());
        ZlFieldMapping keyInfo = fieldMappingMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResFieldMappingDelDTO deleteFieldMappingInfo(ReqDTO<ReqFieldMappingDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingDelDTO reqData = reqInfo.getReqData();
        ResFieldMappingDelDTO res = new ResFieldMappingDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        if(fieldMappingDtlMapper.countExistKey(paramMap) > 0){
            throw new BizException(GlobalRespEnum.DELETION_NOT_ALLOWED);
        }
        
        fieldMappingMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResFieldMappingStateDTO updateFieldMappingState(ReqDTO<ReqFieldMappingStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqFieldMappingStateDTO reqData = reqInfo.getReqData();
        ResFieldMappingStateDTO res = new ResFieldMappingStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlFieldMapping record = new ZlFieldMapping();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        fieldMappingMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("mappingId",record.getMappingId());
        ZlFieldMapping keyInfo = fieldMappingMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}